package com.cn.wanxi.service.impl;

import com.cn.wanxi.common.RedisNumTool;
import com.cn.wanxi.common.ThreadTool;
import com.cn.wanxi.entity.CompanyEntity;
import com.cn.wanxi.entity.ResultEntity;
import com.cn.wanxi.mapper.CompanyMapper;
import com.cn.wanxi.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public ResultEntity find() {
        final CompanyEntity[] companyModel = {new CompanyEntity()};
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                companyModel[0] = findCache();
            }
        });
        thread.start();
        ThreadTool.ThreadSleep(thread);
        return ResultEntity.success(companyModel[0]);
    }

    @Override
    public ResultEntity update(CompanyEntity companyEntity) {
        int result = companyMapper.update(companyEntity);
        if (result > 0) {
            Jedis jedis = new Jedis();
            jedis.select(RedisNumTool.REDIS_COMPANY);
            jedis.del("companyName");
            return ResultEntity.success(result);
        }
        return ResultEntity.failed();
    }

    /**
     * 查找公司是否存在缓存
     *
     * @return
     */
    private synchronized CompanyEntity findCache() {
        CompanyEntity companyModel = new CompanyEntity();
        Jedis jedis = new Jedis();
        jedis.select(RedisNumTool.REDIS_COMPANY);
        boolean cache = jedis.exists("companyName");
        if (cache) {
            companyModel.setCompanyName(jedis.get("companyName"));
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setUnifiedPhone(jedis.get("unifiedPhone"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setFax(jedis.get("fax"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setCopyright(jedis.get("copyright"));
            companyModel.setNumber(jedis.get("number"));
            companyModel.setIntroduction(jedis.get("introduction"));
        } else {
            companyModel = companyMapper.find();
            jedis.set("companyName", companyModel.getCompanyName());
            jedis.set("logo", companyModel.getLogo());
            jedis.set("unifiedPhone", companyModel.getUnifiedPhone());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("email", companyModel.getEmail());
            jedis.set("fax", companyModel.getFax());
            jedis.set("address", companyModel.getAddress());
            jedis.set("copyright", companyModel.getCopyright());
            jedis.set("number", companyModel.getNumber());
            jedis.set("introduction", companyModel.getIntroduction());
        }
        return companyModel;
    }
}
